package com.cuit.qx.service;


import com.cuit.qx.bean.Menu;
import com.cuit.qx.bean.PageList;
import com.cuit.qx.bean.TreeInfoBean;
import com.cuit.qx.dao.MenuMapper;
import com.cuit.qx.utils.tools.StringUtils;
import com.github.pagehelper.PageHelper;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuInfoService {
	static Logger logger = LogManager.getLogger(MenuInfoService.class.getName());

	@Autowired
	private MenuMapper menuMapper;

	/**
	 * 查询用户下一级菜单
	 * @param node
	 * @param userId
	 * @return
	 */
	public List<Menu> queryHeaderMenuInfoList(String node, String userId) {
		return menuMapper.selectHeaderMenuInfoList(userId);
	}

	/**
	 * 查询左边菜单
	 * 
	 * @param node
	 * @return
	 */
	public Menu getMenusLeftAll(String node, String userId) {
		Menu rootBean = new Menu();
		List<Menu> daoList = menuMapper.selectMenuGroupByUserId(userId);
		rootBean.setMenuId(node);
		Map<String, String> tempMap = new HashMap<String, String>();
		findChild(daoList, rootBean, tempMap);
		return rootBean;
	}


	/**
	 * 重新组装菜单数据
	 * 
	 * @param list
	 * @param parentBean
	 * @param tempMap
	 */
	private void findChild(List<Menu> list, Menu parentBean,
			Map<String, String> tempMap) {
		List<Menu> tempMapList = new ArrayList<Menu>();
		for (Menu bean : list) {
			if (parentBean.getMenuId().equals(bean.getParentId())
					&& bean.getLeaf() < 4) {
				if (tempMap.get(bean.getMenuId()) != null) {
					tempMapList.add(bean);
					continue;
				}
				parentBean.getChildren().add(bean);
				tempMap.put(bean.getMenuId(), bean.getMenuId());
			}
		}
		for (Menu bean : parentBean.getChildren()) {
			list.remove(bean);
			list.removeAll(tempMapList);
			findChild(list, bean, tempMap);
		}
	}

	/**
	 * 查询所有菜单集合
	 * 
	 * @param roleId
	 * @return
	 */
	public TreeInfoBean queryAllMenuByRoleId(String roleId) {
		TreeInfoBean bean = new TreeInfoBean();
		bean.setId("1001");
		bean.setLabel("梧州气象精细化暴雨决策服务平台");
		// 所有的menuInfoBean集合
		List<TreeInfoBean> daoList = new ArrayList<TreeInfoBean>();

		Set<String> set = new HashSet<String>();
		List<Menu> resultList = null;
//		if(roleId == null){
			resultList = menuMapper.selectMenuGroupAll();
//		}else{
//			resultList = menuMapper.selectMenuGroupByRoleId(roleId);
//		}
		
		for (Iterator<Menu> iter = resultList.iterator(); iter.hasNext();) {
			Menu element = iter.next();
			if (set.add(element.getMenuId())) {
				// 转换为树对象数据
				TreeInfoBean newBean = new TreeInfoBean();
				if(roleId != null){
					boolean reslut = this.findStatusRoleMenu(roleId,
							element.getMenuId());
					newBean.setChecked(reslut);
				}
				newBean.setLabel(element.getName());
				newBean.setId(element.getMenuId());
				newBean.setParentId(element.getParentId());
				newBean.setState("open");
				daoList.add(newBean);
			}
		}
		Map<String, String> tempMap = new HashMap<String, String>();
		this.findTreeChild(daoList, bean, tempMap);
		tempMap.clear();
		return bean;
	}


	/**
	 * 查找角色下所有菜单
	 * 
	 * @param selectRoleId
	 * @param menuId
	 * @return
	 */
	public boolean findStatusRoleMenu(String selectRoleId, String menuId) {
		List<HashMap<String, Object>> list = menuMapper.checkMenuGroup(selectRoleId, menuId);
		if (!list.isEmpty() && null != list.get(0).get("LEAF")) {
			//判断是否有下级菜单菜单
			if(menuMapper.checkMenuChildCon(selectRoleId, menuId) > 0){
				return false;
			}else if(Integer.parseInt(String.valueOf(list.get(0).get("STATUS"))) == 0){
				return true;
			}
		}
		return false;
	}

	/**
	 * 查询tree
	 * 
	 * @param list
	 * @param parentBean
	 * @param tempMap
	 */
	private void findTreeChild(List<TreeInfoBean> list,
			TreeInfoBean parentBean, Map<String, String> tempMap) {
		List<TreeInfoBean> tempMapList = new ArrayList<TreeInfoBean>();
		for (TreeInfoBean bean : list) {
			if (parentBean.getId().equals(bean.getParentId())) {
				if (tempMap.get(bean.getId()) != null) {
					tempMapList.add(bean);
					continue;
				}
				parentBean.getChildren().add(bean);
				tempMap.put(bean.getId(), bean.getId());
			}
		}
		for (TreeInfoBean bean : parentBean.getChildren()) {
			list.remove(bean);
			list.removeAll(tempMapList);
			findTreeChild(list, bean, tempMap);
		}
	}

	/**
	 * 查询表格
	 * @param parentId
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public PageList<Menu> queryMenuPageRight(String parentId, int start,
											 int pageSize) {
		PageHelper.startPage(start, pageSize); // 核心分页代码
		List<Menu> list = menuMapper.selectMenuPageRight(parentId);
		return new PageList<Menu>(list);
	}

	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	public int addMenu(Menu menu) {
		int sort = 0;
		try {
			sort = menuMapper.selectMenuMaxSort(menu.getParentId());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		sort++;
		menu.setSort(sort);
		menu.setMenuId(StringUtils.getPrimarykeyId());
		menu.setType("1");
		int num = menuMapper.insertSelective(menu);
		return num;
	}

	/**
	 * 查看菜单是否存在
	 * @param
	 * @return
	 */
	public int checkNameIsExsit(String menuName) {
		return menuMapper.checkNameIsExsit(menuName);
	}

	/**
	 * 
	 * 删除菜的那
	 * @param id
	 * @return
	 */
	public int deleteMenu(String id) {
		return menuMapper.deleteMenu(id);
	}

	/**
	 * 
	 * 通过ID查询菜单
	 * @param id
	 * @return
	 */
	public Menu queryMenuById(String id) {
		return menuMapper.queryMenuById(id);
	}

	/**
	 * 
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	public int updateMenu(Menu menu) {
		return menuMapper.updateMenu(menu);
	}

	/**
	 * 
	 * 改变菜单状态
	 * @param menuId
	 * @param status
	 * @return
	 */
	public int changeMenuStatus(String menuId, String status) {
		return menuMapper.updateMenuStatus(menuId,Integer.parseInt(status));
	}

	
	public List<Menu> getAllMenu(){
		return menuMapper.selectMenuGroupAll();
	}
	
	/**
	 * 获取用户权限菜单名称
	 * @param roleid
	 * @return
	 */
	public List<String> getMenuNameList(String roleid){
		return menuMapper.getMenuNameList(roleid);
	}
	
	
	
}
